﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;


namespace IHISCETeim.Web.MachineSys.FTradeModel
{
    public partial class FTradeAgeDetaiSuspendAdd : PageBase
    {
        protected override string DWZRef
        {
            get
            {
                return "FTradeReceivablesListEasyUI";
            }
        }

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.应收帐款_缓期 };
            }
        }

        protected int FTradeAgeId
        {
            get
            {

                return GetRequestValue<int>("FTradeAgeId", 0);
            }
        }

        private FTradeAgeDetailInfo _objFx = null;

        protected FTradeAgeDetailInfo obj
        {
            get
            {
                if (_objFx == null)
                    _objFx = FTradeAgeDetailController.GetByFTradeAgeId(FTradeAgeId);
                return _objFx;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "add")
            {
                addmenu();
            }
        }

        protected object GetShowIndex(decimal _showIndex, int nowShowIndex)
        {
            //return _showIndex.ToString("f") + nowShowIndex.ToString();

            string showIndex = _showIndex.ToString("F7");
            //小数点后的值
            string x = showIndex.Split(new char[] { '.' })[1];
            string xx = x.Substring(0, x.IndexOf("0"));
            string xxx = showIndex.Split(new char[] { '.' })[0] + "." + xx + nowShowIndex.ToString();
            return xxx;
        }

        private void addmenu()
        {

            
                char[] caSplit = new char[] { ',' };
                string[] ShowIndexs = GetRequestValue<string>("ShowIndex", "").Split(caSplit);
                string[] ShowTitles = GetRequestValue<string>("ShowTitle", "").Split(caSplit);
                string[] JinEs = GetRequestValue<string>("JinE", "0").Split(caSplit);
                string[] DaoQiRis = GetRequestValue<string>("DaoQiRi", "").Split(caSplit);
                string[] TanQeChenBens = GetRequestValue<string>("TanQeChenBen", "").Split(caSplit);
                string[] YearRates = GetRequestValue<string>("YearRate", "").Split(caSplit);
                string[] Remarks = GetRequestValue<string>("Remark", "").Split(caSplit);

               // decimal YearRate = GetRequestValue<decimal>("FTradeAgeDetaiSuspendAddYearRate", 0m);

                List<FTradeAgeDetailInfo> list = new List<FTradeAgeDetailInfo>();
                try
                {
                    string checkerr = "";
                    for (int rowIndex = 0; rowIndex < JinEs.Count(); rowIndex++)
                    {
                        FTradeAgeDetailInfo newAgeObj = new FTradeAgeDetailInfo();
                        if (Convert.ToDecimal(JinEs[rowIndex]) > 0)
                        {
                            newAgeObj.FTradeSalesId = obj.FTradeSalesId;
                            newAgeObj.ShowIndex = Convert.ToDecimal(ShowIndexs[rowIndex]);
                            newAgeObj.TongJiName = obj.TongJiName;
                            newAgeObj.ShowTitle = ShowTitles[rowIndex];
                            newAgeObj.JinE = Convert.ToDecimal(JinEs[rowIndex]);
                            newAgeObj.DaoQiRi = Convert.ToDateTime(DaoQiRis[rowIndex]);
                            if (newAgeObj.DaoQiRi <= obj.DaoQiRi)
                            {
                                checkerr = "新的到期日必需晚于原到期日.";
                                break;
                            }  
                            newAgeObj.SuspendedMark = FTradeAgeId;
                            newAgeObj.RealName = User.RealName;
                            newAgeObj.Remark = Remarks[rowIndex];
                            newAgeObj.YearRate = Convert.ToDecimal(YearRates[rowIndex]); 
                            newAgeObj.TanQeChenBen= Convert.ToDecimal(TanQeChenBens[rowIndex]);
                            TimeSpan ts = newAgeObj.DaoQiRi - obj.DaoQiRi;
                            newAgeObj.YuanQiDay = ts.Days;
                            list.Add(newAgeObj);
                        }
                       // rowIndex++;
                    }
                    if(!string.IsNullOrEmpty(checkerr))
                    {            
                            ErrorJson("新的到期日必需晚于原到期日.");
                    }

             }
                catch (Exception exc)
                {
                    ErrorJson(exc.Message);
                }
                //newAgeObj.FTradeSalesId = obj.FTradeSalesId;
                //newAgeObj.ShowIndex = obj.ShowIndex;
                //newAgeObj.TongJiName = obj.TongJiName;
                //newAgeObj.ShowTitle = obj.ShowTitle;
                //newAgeObj.JinE = obj.JinE;
                //newAgeObj.DaoQiRi = GetRequestValue<DateTime>("newdaoqiri", DateTime.Now);
                //newAgeObj.SuspendedMark = FTradeAgeId;
                //newAgeObj.RealName = User.RealName;
                //newAgeObj.Remark = "";

                int msg = FTradeAgeDetailController.Adds(list);



                if (msg > 0)
                    SuccessJson("成功延期付款", "FTradeReceivablesEdit");
                else
                {

                    ErrorJson("延期付款失败<br/>");
                }

           
        }
    }
}